package com.ecp.commons.file;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateBasedRenamePolicy implements FileRenamePolicy {

	private String	Directory;

	public DateBasedRenamePolicy() {

	}

	private String getDirectory() {
		if (Directory == null) {
			GregorianCalendar cal = new GregorianCalendar();
			cal.setTime(new Date());
			Directory = "";
			Directory += cal.get(Calendar.YEAR);
			Directory += File.separator;
			Directory += cal.get(Calendar.MONTH) + 1;
			Directory += File.separator;
			Directory += cal.get(Calendar.DAY_OF_MONTH);
			return Directory;
		} else {
			return Directory;
		}
	}

	private String getDirectory(File f) {
		String path = f.getPath();
		String name = f.getName();
		return path.substring(0, path.length() - name.length() - 1);
	}

	public File rename(File f) {
		File f1 = new File(getDirectory(f), getDirectory());
		f1.mkdirs();
		File f2 = new File(f1, f.getName());

		return f2;
	}
}
